//
//  ThemeButton.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/21.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "ThemeButton.h"
#import "ThemeManager.h"

@implementation ThemeButton

/**
 * 重写dealloc方法，旨在移除观察者
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}


/**
 * 重写initWithFrame方法，旨在让按钮可以响应切换主题通知
 */
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        /**
         * 注册成为接收者
         */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    }
    
    return self;
    
}


/**
 * 重写awakeFromNib方法，旨在方便XIB创建
 */
- (void)awakeFromNib{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    
}


/**
 * 通知的响应方法
 */
- (void)themeDidChangedNotificationAction:(NSNotification *)noti{
    [self reloadIamge];
#warning ---------------------背景图片暂未设置（[self reloadBgIamge]）
}


//---------------------------------加载图片-----------------------------------------

/**
 * 重新加载图片
 */
- (void)reloadIamge{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取图
     */
    UIImage *normalImage = [manager getThemeImage:self.normalImageName];
    UIImage *highlightImage = [manager getThemeImage:self.highlightImageName];
    
    /**
     * 设置图片
     */
    if (normalImage) {
        [self setImage:normalImage forState:UIControlStateNormal];
    }
    
    if (highlightImage) {
        [self setImage:highlightImage forState:UIControlStateHighlighted];
    }
}


/**
 * 重新加载背景图片
 */
- (void)reloadBgIamge{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取图
     */
    UIImage *normalBgImage = [manager getThemeImage:self.normalImageName];
    UIImage *highlightBgImage = [manager getThemeImage:self.highlightImageName];
    
    /**
     * 设置图片
     */
    if (normalBgImage) {
        [self setBackgroundImage:normalBgImage forState:UIControlStateNormal];
    }
    
    if (highlightBgImage) {
        [self setBackgroundImage:highlightBgImage forState:UIControlStateHighlighted];
    }
}


//----------------------------------更改图片----------------------------------------

/**
 ** 重新set方法，旨在更改button图片
 **/

- (void)setNormalImageName:(NSString *)normalImageName{
    if (![_normalImageName isEqualToString:normalImageName]) {
        _normalImageName = [normalImageName copy];
        
        [self reloadIamge];
    }
}

- (void)setHighlightImageName:(NSString *)highlightImageName{
    if (![_highlightImageName isEqualToString:highlightImageName]) {
        _highlightImageName = [highlightImageName copy];
        
        [self reloadIamge];
    }
}

- (void)setNormalBgImageName:(NSString *)normalBgImageName{
    if (![_normalBgImageName isEqualToString:normalBgImageName]) {
        _normalBgImageName = [normalBgImageName copy];
        
        [self reloadBgIamge];
    }
}

- (void)setHighlightBgImageName:(NSString *)highlightBgImageName{
    if (![_highlightBgImageName isEqualToString:highlightBgImageName]) {
        _highlightBgImageName = [highlightBgImageName copy];
        
        [self reloadBgIamge];
    }
}

//--------------------------------------------------------------------------




















/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
